#!/usr/bin/python
# -*- coding: UTF-8 -*- 

import wx
import sys
#sys.path.append('./Common/')
import os
import zipfile
currentFilePath = __file__
parentPath = os.path.dirname(currentFilePath)
oriPath = os.path.dirname(parentPath)
sys.path.append(oriPath)

from copyZip import copyDir
import panelOne,panelTwo
#import listDir

class Notebook(wx.Notebook):
    def __init__(self,parent):
        wx.Notebook.__init__(self,parent,id=wx.ID_ANY,style=
                 wx.BK_DEFAULT
                 #wx.BK_TOP
                 #wx.BK_BOTTOM
                 #wx.BK_LEFT
                 #wx.BK_RIGHT
                 )
      
        tabOne = panelOne.TabPanel(self)
        #tabOne.checkRes()
        #tabOne.SetBackgroundColour("Gray")
        self.AddPage(tabOne,"贴纸生成")

    
        tabTwo = panelTwo.TabPanel(self)
        self.AddPage(tabTwo,"贴纸调试打包")



        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED,self.OnPageChanged)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING,self.OnPageChanging)

    def OnPageChanged(self,event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.GetSelection()
        print 'OnPageChanging,old:%d,new:%d,sel:%d\n' %(old,new,sel)
        event.Skip()

    def OnPageChanging(self,event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.GetSelection()
        print 'OnPageChanging,old:%d,new:%d,sel:%d\n' %(old,new,sel)
        event.Skip()
        
class MYFrame(wx.Frame):
    '''
    Frame that holds all other widgets
    '''
    def __init__(self,parent,ID,title):
        wx.Frame.__init__(self,parent,ID,title=title,size=(500,700))
        
        self.__BuildMenus()
        panel = wx.Panel(self,-1)

        notebook = Notebook(panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook,1,wx.ALL|wx.EXPAND,5)
        panel.SetSizer(sizer)
        self.Layout()
    def __BuildMenus(self):
        mainMenuBar = wx.MenuBar()
        
        fileMenu = wx.Menu()
        
        fileMenuItem = fileMenu.Append(-1, "更新模版文件")
        self.Bind(wx.EVT_MENU, self.__OpenSingleFile, fileMenuItem)
        
        mainMenuBar.Append(fileMenu, title =u'&贴纸模版')
        
        self.SetMenuBar(mainMenuBar)  
    def __OpenSingleFile(self, event):
        filesFilter = "*.zip"
        fileDialog = wx.FileDialog(self, message ="选择模版资源(.zip)", wildcard = filesFilter, style = wx.FD_OPEN)
        dialogResult = fileDialog.ShowModal()
        if dialogResult !=  wx.ID_OK:
            return
        path = fileDialog.GetPath()
        templatePath = oriPath+"/Template"
        copyDir("default",path,templatePath)
        
        file_list = os.listdir(templatePath)
        for file_name in file_list:
            if os.path.splitext(file_name)[1] == '.zip':
                print(file_name)
                file_zip = zipfile.ZipFile(templatePath+"/"+file_name, 'a')
                for file in file_zip.namelist():
                    file_zip.extract(file, templatePath)
                file_zip.close()
                os.remove(templatePath+"/"+file_name)  
        #print(file_list)
        # filesFilter = "*.zip"
        # fileDialog = wx.FileDialog(self, message ="选择模版资源(.zip)", wildcard = filesFilter, style = wx.FD_OPEN)
        # dialogResult = fileDialog.ShowModal()
        # if dialogResult !=  wx.ID_OK:
        #     return
        # path = fileDialog.GetPath()
        # 
        # copyDir("default",path,templatePath)
        #拷贝到目录下
        #self.__TextBox.SetLabel(path)
class App(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        
    def OnInit(self):
        self.title = "贴纸自动化生成工具"
        self.frame = MYFrame(None,-1,self.title)
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        return True

if __name__ == "__main__":
    myApp = App()
    myApp.MainLoop()